package com.yiwei.zhh.adapter;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.yiwei.zhh.R;
import com.yiwei.zhh.interfaces.OnRecyclerViewListener;
import com.yiwei.zhh.model.ContentDetailModel;

import java.util.List;


/**
 * SOS明细
 * Created by MasterDong on 16/4/12.
 */

public class HelpAdapter extends RecyclerView.Adapter {


    private OnRecyclerViewListener onRecyclerViewListener;

    public void setOnRecyclerViewListener(OnRecyclerViewListener onRecyclerViewListener) {
        this.onRecyclerViewListener = onRecyclerViewListener;
    }

    private Context mContext;
    private List<ContentDetailModel> list;

    public HelpAdapter(Context context, List<ContentDetailModel> list) {
        this.list = list;
        this.mContext = context;
    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup viewGroup, int i) {
        View view = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.help_items, null);
        LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);
        view.setLayoutParams(lp);
        return new HelpViewHolder(view);
    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder viewHolder, int i) {
        HelpViewHolder holder = (HelpViewHolder) viewHolder;
        holder.position = i;
        ContentDetailModel model = list.get(i);
        holder.type.setText(model.getType());
        holder.title.setText(model.getTitle());
        if (TextUtils.isEmpty(model.getType())) holder.type.setVisibility(View.GONE);
        else holder.type.setVisibility(View.VISIBLE);

    }

    /**
     * 添加数据
     *
     * @param list
     */
    public void addList(List<ContentDetailModel> list) {
        this.list.addAll(list);
    }

    public void clearList() {
        this.list.clear();
    }

    @Override
    public int getItemCount() {
        return list.size();
    }

    class HelpViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener, View.OnLongClickListener {
        public View rootView;
        public TextView title;
        public TextView type;
        public int position;

        public HelpViewHolder(View itemView) {
            super(itemView);
            title = (TextView) itemView.findViewById(R.id.title);
            type = (TextView) itemView.findViewById(R.id.type);

            rootView = itemView.findViewById(R.id.loan_record_item);
            rootView.setOnClickListener(this);
            rootView.setOnLongClickListener(this);
        }

        @Override
        public void onClick(View v) {
            if (null != onRecyclerViewListener) {
                onRecyclerViewListener.onItemClick(list, position);
            }
        }

        @Override
        public boolean onLongClick(View v) {
            if (null != onRecyclerViewListener) {
                return onRecyclerViewListener.onItemLongClick(list, position);
            }
            return false;
        }

    }
}

